#include "../../include/client_handle.h"

/***************************************************
函数名:handlerecvmsg
功能:消息接收
传入参数:int *sockfd
返回值:无
***************************************************/
void handlerecvmsg(int *sockfd)
{
    int connfd = *sockfd;
    int nread;
    char buf[1024];
    char str[1024];
    struct message recvmsg;
    time_t timep;                              //显示当前时间
    if((fd=my_open(chat_log,O_RDWR|O_CREAT|O_APPEND,0777)) < 0)          //创建聊天记录文件    
    {
        printf("打开聊天记录失败!");
        exit(1);
    }
    while(1)
    {
        nread = my_recv(connfd,&recvmsg,sizeof(struct message),0);    //接受文件     
        if(nread == 0)
        {
            printf("You have detached from the server!\n");     
            my_close(fd);
            my_close(connfd);
            exit(0);
        }
        else if (my_strcmp(recvmsg.flag,"all") == 0)             //接受群发信息
        {
            time (&timep);
	    memset(str,0,strlen(str));
            sprintf(str,"%s%s 对大家说: %s\n",ctime(&timep),recvmsg.name,recvmsg.msg);
	    printf("%s",str);
	    my_write(fd,str,strlen(str));                          //聊天信息写入文件
        }
        else if (my_strcmp(recvmsg.flag,"personal") == 0)         //接受私信 
        {
            time (&timep);
	    memset(str,0,strlen(str));
            sprintf(str,"%s%s 对你说: %s\n",ctime(&timep),recvmsg.name,recvmsg.msg);
	    printf("%s",str);
	    my_write(fd,str,strlen(str));                         //保存聊天记录
        }
	else if (my_strcmp(recvmsg.flag,"sermsg") == 0)          //系统提示信息
        {
            time (&timep);
	    memset(str,0,strlen(str));
            sprintf(str,"%s系统信息: %s\n",ctime(&timep),recvmsg.msg);
	    printf("%s",str);
	    my_write(fd,str,strlen(str));
	    continue;
        }
        else if (my_strcmp(recvmsg.flag,"view") == 0)       //收到查看在线用户标志
        {
            time (&timep);
	    memset(str,0,strlen(str));
            sprintf(str,"%s在线用户:\n%s\n",ctime(&timep),recvmsg.msg);
	    printf("%s",str);
            continue;
        }
        else if (my_strcmp(recvmsg.flag,"trans") == 0)          //传输文件标志
        {
            pthread_t pid;
            if (my_strcmp(recvmsg.msg,"agree") == 0)          //同意接受文件标志
            {
                my_strcpy(filefromname,recvmsg.name);
		printf("文件传送中……\n");
                //创建线程发送文件
                pthread_create(&pid,NULL,(void *)handlesendfile,NULL);     //创建发送文件线程
            }
            else if(my_strcmp(recvmsg.msg,"disagree") == 0)
            {
                printf("对方拒绝接收文件\n");
                my_close(savefilefd);
                savefilefd = -1;
            }
            else if(my_strcmp(recvmsg.msg,"noexist") == 0)
            {
                printf("对不起，该客户端不存在\n");
                my_close(savefilefd);
                savefilefd = -1;
            }
            else
            {
                my_strcpy(filefromuser,recvmsg.name);
                my_strcpy(filefromname,recvmsg.msg);
                printf("%s向你传文件%s,是否同意接受?\n同意请输入：trans$agree\t不同意请输入：trans$disagree\n",recvmsg.name,recvmsg.msg);
                savefilefd = 0;
            }
            continue;
        }
        else if(my_strcmp(recvmsg.flag,"transf") == 0)               
        {
            pthread_t pid_transf;
	    pthread_create(&pid_transf,NULL,(void *)handlerecvfile,&recvmsg); //创建文件传输线程
	    pthread_join(pid_transf,NULL);			
            continue;
        }
        else
        {
            if(my_strcmp(recvmsg.flag,"") == 0)
            {
                continue;
            }
        }
    }
}

